
#pragma once

#include "MathCommon.h"


class vec2
{
public:
    union
    {
        struct
        {
            float x, y;
        };
        struct
        {
            float r, g;
        };
    };
    
    vec2():                   x(0), y(0)		 {}
    vec2(float xv, float yv): x(xv), y(yv)		 {}
    vec2(const vec2& vec):    x(vec.x), y(vec.y) {}
    ~vec2() {}
    
    float length();
	vec2& normalize();
    
    vec2& operator = (const vec2& vec);
    vec2& operator - ();
    
    friend bool		   operator == (const vec2& left, const vec2& right);
    friend vec2&	   operator += (vec2& left, const vec2& right);
    friend vec2&	   operator -= (vec2& left, const vec2& right);
	friend vec2&	   operator *= (vec2& left, const float right);
    friend const vec2  operator +  (const vec2& left, const vec2& right);
    friend const vec2  operator -  (const vec2& left, const vec2& right);
    friend const vec2  operator *  (const vec2& vec, const float right);
    friend const float operator *  (const vec2& left, const vec2& val);
};



inline float vec2::length()
{
    return sqrt(x * x + y * y);
}

inline vec2& vec2::normalize()
{
	float invLen = 1.0f / sqrt(x * x + y * y);

	x *= invLen;
	y *= invLen;

	return *this;
}

inline vec2& vec2::operator = (const vec2& vec)
{
    x = vec.x;
    y = vec.y;

    return *this;
}

inline vec2& vec2::operator - ()
{
    x *= -1;
    y *= -1;

    return *this;
}

inline bool operator == (const vec2& left, const vec2& right)
{
    return (left.x == right.x) && (left.y == right.y);
}

inline vec2& operator += (vec2& left, const vec2& right)
{
    left.x += right.x;
    left.y += right.y;

    return left;
}

inline vec2& operator -= (vec2& left, const vec2& right)
{
    left.x -= right.x;
    left.y -= right.y;

    return left;
}

inline vec2& operator *= (vec2& left, const float right)
{
	left.x *= right;
    left.y *= right;

    return left;
}

inline const vec2 operator + (const vec2& left, const vec2& right)
{
    vec2 res(left.x + right.x, left.y + right.y);

    return res;
}

inline const vec2 operator - (const vec2& left, const vec2& right)
{
    vec2 res(left.x - right.x, left.y - right.y);

    return res;
}

inline const float operator * (const vec2& left, const vec2& right)
{
    return left.x * right.x + left.y * right.y;
}

inline const vec2 operator * (const vec2& vec, const float val)
{
    vec2 res(vec.x * val, vec.y * val);

    return res;
}